<template>
  <div
    :ref="refName"
    :min="min"
    :max="max"
    :top="top"
    :right="right"
    :x="x"
    :y="y"
    :optionData="optionData"
  ></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  props: {
    refName: {
      type: String,
      default: "ref",
    },
    min: {
      type: Number,
      default: 60,
    },
    max: {
      typeof: Number,
      default: 120,
    },
    top: {
      type: String,
      default: "center",
    },
    right: {
      type: String,
      default: "0",
    },
    x: {
      type: String,
      default: "36%",
    },
    y: {
      type: String,
      default: "50%",
    },
    optionData: {
      type: Array,
    },
  },
  data() {
    return {
      myChart: null,
    };
  },
  watch: {
    optionData: {
      handler(newVal, oldVal) {
        this.chartData = newVal;
        this.init();
      },
    },
  },
  methods: {
    init() {
      if (this.myChart) {
        this.myChart.clear();
        this.myChart.dispose();
        this.myChart = null;
      }
      this.myChart = echarts.init(this.$refs[this.refName]);
      let option = {
        legend: {
          orient: "vertical",
          top: this.top,
          right: this.right,
          itemGap: 20,
        },
        tooltip: {
          trigger: "item",
          formatter: "{b} : {c} ({d}%)",
        },
        series: [
          {
            type: "pie",
            radius: [this.min, this.max],
            center: [this.x, this.y],
            roseType: "radius",
            label: {
              show: false,
            },
            data: this.chartData,
          },
        ],
      };
      option && this.myChart.setOption(option);
    },
  },
};
</script>
